package com.luckynote.common.interceptor;

import com.luckynote.common.base.config.AppConfig;
import com.luckynote.common.base.dubbo.LogEnum;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * <pre>
 * *********************************************
 * Copyright LUCKY-NOTE.
 * All rights reserved.
 * ****************************************************
 *  Version       Date         Author            Desc
 *    v1.0       2019/7/7      bingo        自定义拦截器
 * ****************************************************
 * </pre>
 */
//@Component
public class MyInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String traceId = UUID.randomUUID().toString().replace("-", "");
        MDC.put(LogEnum.TRACE_ID.getCode(),traceId);
        MDC.put(LogEnum.MODULE_NAME.getCode(),AppConfig.getProjectName());
        return true;
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        MDC.clear();
    }

}
